package test;

import model.Graph;
import parser.GraphFilePxPosition;
import parser.GraphGenerator;
import parser.ParserGraph;
import view.GUIHelper;
import view.Window;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * Created with IntelliJ IDEA.
 * User: Thomas
 * Date: 18/02/14
 * Time: 09:01
 */
public class Application {
    private static Application ourInstance = new Application();

    private Graph graph;
    private Window window = new Window();

    public static Application getInstance() {
        return ourInstance;
    }

    private Application() {
        try {
            this.graph = ParserGraph.parse(Test.getGraphFile().getGraphFilename());
            GraphFilePxPosition.read(this.graph);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void show() {
        //Initialisation du canvas
        this.getWindow().initCanvas();

        //Ouverture de la fenetre
        GUIHelper.showOnFrame(this.getWindow().getjCanvas(), "test JCanvas");
    }

    public Graph getGraph() {
        return graph;
    }

    public static Graph getGraphS() {
        return Application.getInstance().getGraph();
    }

    public void setGraph(Graph graph) {
        this.graph = graph;
    }

    public Window getWindow() {
        return window;
    }

    public static Window getWindowS() {
        return Application.getInstance().getWindow();
    }

    public void setWindow(Window window) {
        this.window = window;
    }

    public void generateGraph(Integer integer) {
        try {
            Test.setGraphFile(Config.GraphFile.TEST);
            Graph graph1 = new GraphGenerator(integer).generate(Test.getGraphFile().getGraphFilename());

            this.graph = ParserGraph.parse(Test.getGraphFile().getGraphFilename());
            GraphFilePxPosition.read(this.graph);

            this.getWindow().initCanvas();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
